package com.netease.nie.service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dao.ExchangeGoodsInfoExtendDao;
import com.netease.nie.model.ExchangeGoodsInfoExtend;

@Service
public class ExchangeGoodsInfoExtendServiceImpl implements ExchangeGoodsInfoExtendService {

	@Autowired
	private ExchangeGoodsInfoExtendDao exchangeGoodsInfoExtendDao;
	
	public ExchangeGoodsInfoExtend queryGoodsInfoExtendById(int id) {
		ExchangeGoodsInfoExtend exchangeGoodsInfoExtend = exchangeGoodsInfoExtendDao.queryGoodsInfoExtendById(id);
		if (exchangeGoodsInfoExtend == null) 
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		return exchangeGoodsInfoExtend;
	}
	
	public Map<Integer, ExchangeGoodsInfoExtend> queryGoodsInfoExtendByIds(List<Integer> ids) {
		List<ExchangeGoodsInfoExtend> exchangeGoodsInfoExtendList = exchangeGoodsInfoExtendDao.queryGoodsInfoExtendByIds(ids);
		if(exchangeGoodsInfoExtendList == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		
		if (exchangeGoodsInfoExtendList == null || exchangeGoodsInfoExtendList.isEmpty())
			return Collections.emptyMap();
		
		Map<Integer, ExchangeGoodsInfoExtend> goodsInfoExtendMap = new HashMap<>();
		
		int size = exchangeGoodsInfoExtendList.size();
		int goodsId;
		ExchangeGoodsInfoExtend exchangeGoodsInfoExtend = null;
		
		for (int i = 0; i < size; ++i) {
			exchangeGoodsInfoExtend = exchangeGoodsInfoExtendList.get(i);
			goodsId = exchangeGoodsInfoExtend.getId();
			goodsInfoExtendMap.put(goodsId, exchangeGoodsInfoExtend);
		}
		
		return goodsInfoExtendMap;
	}

	public ExchangeGoodsInfoExtend queryGoodsIntroductionById(int id) {
		ExchangeGoodsInfoExtend exchangeGoodsInfoExtend = exchangeGoodsInfoExtendDao.queryGoodsInfoExtendById(id);
		if (exchangeGoodsInfoExtend == null) 
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		return exchangeGoodsInfoExtend;
	}
	
}
